#include <bits/stdc++.h>
// 2025/04/05
// tag: 
// Author: Forever
using namespace std;
using int64 = int64_t;

int n, m, q, ans;
vector<int> g[400001];
bool vis[400001];

void dfs(int x){
    vis[x] = true;
    for (int it : g[x]) 
        if (!vis[it])
            dfs(it);
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> q;
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v + n);
        g[v + n].push_back(u);
    }
    for (int i = 1; i <= n + m; i++) {
        if (!vis[i]) {
            dfs(i);
            ans++;
        }
    }
    cout << ans - 1 << endl;
    return 0;
}